package com.bawei.demo_shoppingcar.activity;

import android.app.Instrumentation;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bawei.demo_shoppingcar.R;
import com.bawei.demo_shoppingcar.application.ContentData;
import com.bawei.demo_shoppingcar.utils.DataClearManager;

import java.io.File;

public class SettingsActivity extends AppCompatActivity {

    private RelativeLayout relative_cache;
    private TextView cache_text;
    public String TAG = "SettingsActivity";
    private DataClearManager manager;
    private File cacheDir;
    private ImageView back_normal;
    private RelativeLayout setting_tel;
    private Button button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        manager = new DataClearManager();
        initView();

        if (ContentData.isLogin_QQ()){
            button.setVisibility(View.VISIBLE);
        }else{
            button.setVisibility(View.GONE);
        }
    }

    private void initView() {
        button = (Button) findViewById(R.id.setting_exit);
        setting_tel = (RelativeLayout) findViewById(R.id.setting_tel);
        setting_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                call("0212-2351004");
            }
        });
        back_normal = (ImageView) findViewById(R.id.back_normal);
        relative_cache = (RelativeLayout) findViewById(R.id.relative_cache);
        cache_text = (TextView) findViewById(R.id.cache_text);
        cacheDir = getCacheDir();
        back_normal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        Instrumentation inst = new Instrumentation();
                        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                    }
                }.start();
            }
        });

        try {
            String cacheSize = manager.getCacheSize(cacheDir);
            cache_text.setText("已经缓存了" + cacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        relative_cache.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                manager.deleteFolderFile(cacheDir.toString(), true);
                Toast.makeText(getApplicationContext(), "删除缓存成功", Toast.LENGTH_SHORT).show();
                cache_text.setText("缓存了0.0KB");
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentData.setIsLogin_QQ(false);
                finish();
            }
        });
    }
    /**
     * 调用拨号功能
     * @param phone 电话号码
     */
    private void call(String phone) {
        Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone));
        startActivity(intent);
    }

}
